package com.fr.report.web.ui;

import com.fr.base.ArrayUtils;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.core.ReportUtils;
import com.fr.report.script.Calculator;
import com.fr.report.web.event.Listener;
import com.fr.report.web.ui.container.Container;
import com.fr.web.Repository;
import com.fr.web.core.SessionIDInfor;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public abstract class Widget
  implements XMLable, Component
{
  public static final String XML_TAG = "Widget";
  protected boolean disabled;
  protected boolean invisible;
  protected int width = -1;
  protected int height = -1;
  protected int x = -1;
  protected int y = -1;
  protected Container parent;
  protected String widgetName;
  protected List listenerList;

  public abstract String getXType();

  public abstract boolean isEditor();

  public abstract String[] supportedEvents();

  public int getWidth()
  {
    return this.width;
  }

  public void setWidth(int paramInt)
  {
    this.width = paramInt;
  }

  public int getHeight()
  {
    return this.height;
  }

  public void setHeight(int paramInt)
  {
    this.height = paramInt;
  }

  public int getX()
  {
    return this.x;
  }

  public void setX(int paramInt)
  {
    this.x = paramInt;
  }

  public int getY()
  {
    return this.y;
  }

  public void setY(int paramInt)
  {
    this.y = paramInt;
  }

  public void setLocation(int paramInt1, int paramInt2)
  {
    setX(paramInt1);
    setY(paramInt2);
  }

  public void setBounds(Rectangle paramRectangle)
  {
    setX(paramRectangle.x);
    setY(paramRectangle.y);
    setWidth(paramRectangle.width);
    setHeight(paramRectangle.height);
  }

  public String getWidgetName()
  {
    return this.widgetName;
  }

  public void setWidgetName(String paramString)
  {
    this.widgetName = paramString;
  }

  public boolean isDisabled()
  {
    return this.disabled;
  }

  public void setDisabled(boolean paramBoolean)
  {
    this.disabled = paramBoolean;
  }

  public boolean isInvisible()
  {
    return this.invisible;
  }

  public void setInvisible(boolean paramBoolean)
  {
    this.invisible = paramBoolean;
  }

  public void addListener(Listener paramListener)
  {
    if (paramListener == null)
      return;
    String[] arrayOfString = supportedEvents();
    if (ArrayUtils.isEmpty(arrayOfString))
      return;
    for (int i = 0; i < arrayOfString.length; ++i)
      if (arrayOfString[i].equals(paramListener.getName()))
      {
        if (this.listenerList == null)
          this.listenerList = new ArrayList();
        this.listenerList.add(paramListener);
        return;
      }
  }

  public Listener getListener(int paramInt)
  {
    return ((this.listenerList == null) ? null : (Listener)this.listenerList.get(paramInt));
  }

  public int getListenerSize()
  {
    return ((this.listenerList == null) ? 0 : this.listenerList.size());
  }

  public void clearListeners()
  {
    if (this.listenerList != null)
    {
      this.listenerList.clear();
      this.listenerList = null;
    }
  }

  public JSONArray createJSONData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    JSONArray localJSONArray = new JSONArray();
    return localJSONArray;
  }

  public Listener[] createListeners(Repository paramRepository)
  {
    return ((this.listenerList == null) ? new Listener[0] : (Listener[])this.listenerList.toArray(new Listener[this.listenerList.size()]));
  }

  public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    localJSONObject.put("type", getXType());
    if (paramObject != null)
      localJSONObject.put("value", value2Config(paramObject));
    if (this.width != -1)
      localJSONObject.put("width", this.width);
    if (this.height != -1)
      localJSONObject.put("height", this.height);
    if (this.x != -1)
      localJSONObject.put("top", this.x);
    if (this.y != -1)
      localJSONObject.put("left", this.y);
    localJSONObject.put("widgetname", this.widgetName);
    localJSONObject.put("fieldName", this.widgetName);
    localJSONObject.put("disabled", this.disabled);
    localJSONObject.put("invisible", this.invisible);
    if (paramRepository.getSessionIDInfor() != null)
    {
      localObject = ReportUtils.getWidgetImportedCssFromWorkBook(paramRepository.getSessionIDInfor().getContextBook());
      if (localObject.length > 0)
        localJSONObject.put("widgetCss", localObject);
    }
    Object localObject = createJSONListener(paramRepository);
    if (((JSONArray)localObject).length() > 0)
      localJSONObject.put("listeners", localObject);
    return ((JSONObject)localJSONObject);
  }

  protected Object value2Config(Object paramObject)
  {
    return paramObject;
  }

  private JSONArray createJSONListener(Repository paramRepository)
    throws JSONException
  {
    JSONArray localJSONArray = new JSONArray();
    Listener[] arrayOfListener = createListeners(paramRepository);
    for (int i = 0; i < arrayOfListener.length; ++i)
      localJSONArray.put(arrayOfListener[i].createJSONConfig(paramRepository));
    return localJSONArray;
  }

  public String toString()
  {
    String str = super.getClass().getName();
    return str.substring(str.lastIndexOf(".") + 1);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
      clearListeners();
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      String str2 = null;
      if (str1.equals("Disable"))
      {
        this.disabled = Boolean.valueOf(paramXMLableReader.getElementValue()).booleanValue();
      }
      else if (str1.equals("WidgetName"))
      {
        this.widgetName = paramXMLableReader.getAttr("name");
      }
      else if (str1.equals("WidgetAttr"))
      {
        this.disabled = Boolean.valueOf(paramXMLableReader.getAttr("disabled")).booleanValue();
        this.invisible = Boolean.valueOf(paramXMLableReader.getAttr("invisible")).booleanValue();
        if ((str2 = paramXMLableReader.getAttr("width")) != null)
          this.width = Integer.parseInt(str2);
        if ((str2 = paramXMLableReader.getAttr("height")) != null)
          this.height = Integer.parseInt(str2);
      }
      else if (str1.equals("Listener"))
      {
        Listener localListener = new Listener();
        paramXMLableReader.readXMLObject(localListener);
        addListener(localListener);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i;
    if (StringUtils.isNotEmpty(this.widgetName))
      paramXMLPrintWriter.startTAG("WidgetName").attr("name", this.widgetName).end();
    paramXMLPrintWriter.startTAG("WidgetAttr");
    if (this.disabled)
      paramXMLPrintWriter.attr("disabled", this.disabled);
    if (this.invisible)
      paramXMLPrintWriter.attr("invisible", this.invisible);
    if (this.width != -1)
      paramXMLPrintWriter.attr("width", this.width);
    if (this.height != -1)
      paramXMLPrintWriter.attr("height", this.height);
    paramXMLPrintWriter.end();
    if (this.listenerList != null)
      for (i = 0; i < this.listenerList.size(); ++i)
        ((Listener)this.listenerList.get(i)).writeXML(paramXMLPrintWriter);
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof Widget) && (ComparatorUtils.equals(((Widget)paramObject).widgetName, this.widgetName)) && (this.disabled == ((Widget)paramObject).disabled) && (this.invisible == ((Widget)paramObject).invisible) && (this.width == ((Widget)paramObject).width) && (this.height == ((Widget)paramObject).height) && (ComparatorUtils.equals(this.listenerList, ((Widget)paramObject).listenerList)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    Widget localWidget = (Widget)super.clone();
    if (this.listenerList != null)
    {
      localWidget.listenerList = new ArrayList();
      int i = 0;
      int j = this.listenerList.size();
      while (i < j)
      {
        localWidget.listenerList.add(((Listener)this.listenerList.get(i)).clone());
        ++i;
      }
    }
    return localWidget;
  }
}